package com.handu.luna.core.domain.support;

import org.apache.commons.beanutils.converters.AbstractConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class EnumConverter extends AbstractConverter {
	
	protected Log logger = LogFactory.getLog(this.getClass());

    @SuppressWarnings("rawtypes")
	@Override
    protected String convertToString(final Object pValue) throws Throwable {
        return ((Enum) pValue).name();
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
    protected Object convertToType(final Class pType, final Object pValue)
        throws Throwable
    {
        // NOTE: Convert to String is handled elsewhere

        final Class<? extends Enum> type = pType;
        try {
            return Enum.valueOf(type, pValue.toString());
        } catch (final IllegalArgumentException e) {
        	logger.warn("No enum value \""
                + pValue
                + "\" for "
                + type.getName());
        }

        return null;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
    protected Class getDefaultType() {
        return null;
    }

}